package com.leetcodehot.problems;

public class problems1669 {
    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
        int count = 0;
        ListNode cur = list1;
        while (count != a - 1) {
            cur = cur.next;
            count++;
        }
        ListNode p0 = cur;
        while (count != b) {
            cur.next = cur.next.next;
            count++;
        }
        ListNode p1 = cur.next;
        p0.next = list2;
        while (list2.next != null) {
            list2 = list2.next;
        }
        list2.next = p1;
        return list1;
    }
}
